﻿// <copyright file="Bill.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-05-28</date>
// <summary>Bill class definition</summary>

namespace NYTimes.Data
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Xml.Linq;

    /// <summary>
    /// Congressional Bill
    /// </summary>
    public class Bill
    {
        /// <summary>
        /// Initializes a new instance of the Bill class.
        /// </summary>
        public Bill()
        {
        }

        /// <summary>
        /// Initializes a new instance of the Bill class.
        /// </summary>
        /// <param name="results">the XML element</param>
        /// <param name="congress">the congress number</param>
        public Bill(XElement results, int congress)
        {
            if (results == null)
            {
                throw new ArgumentNullException("results");
            }

            this.Id = results.Element("number").Value;
            this.BillUri = new Uri(results.Element("bill_uri").Value, UriKind.Absolute);
            this.Title = results.Element("title").Value;
            this.SponsorUri = new Uri(results.Element("sponsor_uri").Value, UriKind.Absolute);
            this.Introduced = DateTime.Parse(results.Element("introduced_date").Value, CultureInfo.InvariantCulture);
            this.Cosponsors = int.Parse(results.Element("cosponsors").Value, CultureInfo.InvariantCulture);
            this.Committees = results.Element("committees").Value.Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
            this.LatestMajorActionDate = DateTime.Parse(results.Element("latest_major_action_date").Value, CultureInfo.InvariantCulture);
            this.LatestMajorAction = results.Element("latest_major_action").Value;

            this.Congress = congress;

            var cosponsoredDate = results.Element("cosponsored_date");

            if (cosponsoredDate != null)
            {
                if (!string.IsNullOrEmpty(cosponsoredDate.Value))
                {
                    this.CosponsoredDate = DateTime.Parse(cosponsoredDate.Value, CultureInfo.InvariantCulture);
                }
            }

            ////this.HousePassageVote = string.IsNullOrEmpty(results.Element("house_passage_vote").Value) ? new DateTime?() : DateTime.Parse(results.Element("house_passage_vote").Value, CultureInfo.InvariantCulture);
            ////this.SenatePassageVote = string.IsNullOrEmpty(results.Element("senate_passage_vote").Value) ? new DateTime?() : DateTime.Parse(results.Element("senate_passage_vote").Value, CultureInfo.InvariantCulture);
            ////this.Sponsor = results.Element("sponsor").Value;

            this.SponsorId = results.Element("sponsor_id") == null ? null : results.Element("sponsor_id").Value;
            this.UrlNumber = results.Element("url_number") == null ? null : results.Element("url_number").Value;
        }

        /// <summary>
        /// Gets or sets the Bill URI
        /// </summary>
        public Uri BillUri { get; set; }

        /// <summary>
        /// Gets or sets the Sponsor URI
        /// </summary>
        public Uri SponsorUri { get; set; }

        /// <summary>
        /// Gets or sets the congress number
        /// </summary>
        public int Congress { get; set; }

        /// <summary>
        /// Gets or sets the bill ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// Gets or sets the URL number
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "This is what the XML element is named.")]
        public string UrlNumber { get; set; }

        /// <summary>
        /// Gets or sets the title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the sponsor of the bill
        /// </summary>
        public string Sponsor { get; set; }

        /// <summary>
        /// Gets or sets the sponsor ID
        /// </summary>
        public string SponsorId { get; set; }

        /// <summary>
        /// Gets or sets the date introduced
        /// </summary>
        public DateTime Introduced { get; set; }

        /// <summary>
        /// Gets or sets the cosponsors
        /// </summary>
        public int Cosponsors { get; set; }

        /// <summary>
        /// Gets or sets the committees
        /// </summary>
        public IEnumerable<string> Committees { get; set; }

        /// <summary>
        /// Gets or sets the latest major action date
        /// </summary>
        public DateTime LatestMajorActionDate { get; set; }

        /// <summary>
        /// Gets or sets the latest major action
        /// </summary>
        public string LatestMajorAction { get; set; }

        /// <summary>
        /// Gets or sets the house passage vote
        /// </summary>
        public DateTime? HousePassageVote { get; set; }

        /// <summary>
        /// Gets or sets the senate passage vote
        /// </summary>
        public DateTime? SenatePassageVote { get; set; }

        /// <summary>
        /// Gets or sets the relationship to another bill
        /// </summary>
        public string Relationship { get; set; }

        /// <summary>
        /// Gets or sets the date the bill was cosponsored
        /// </summary>
        public DateTime? CosponsoredDate { get; set; }

        /// <summary>
        /// returns a string with all of the non-null properties and values
        /// </summary>
        /// <returns>a string with all of the non-null properties and values</returns>
        public override string ToString()
        {
            return Synergist.Utility.GetProperties(this);
        }
    }
}
